/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clientClasses;

import commonClasses.Messages.NewBidBroadcastMsg;
import commonClasses.*;
import commonClasses.Messages.Msg;
import commonClasses.Messages.NewBidMsg;
import commonClasses.Messages.NakMsg;
import commonClasses.commonInterfaces.ClientModelIntf;
import commonClasses.commonInterfaces.ItemIntf;
import commonClasses.commonInterfaces.ServerIntf;
import commonClasses.commonInterfaces.ProtocolIntf;

/**
 *
 * @author Pozytywny!
 */
public class NewBidOrder {

    private ProtocolIntf _clientProtocol = null;
    private ClientModelIntf _model = null;
    private ItemIntf _item = null;

    public NewBidOrder(ProtocolIntf cp, ItemIntf item) {
        _clientProtocol = cp;
        _model = _clientProtocol.getClientModel();
        _item = item;
    }

    public void go() {
        try {
            NewBidMsg newBidMsg = new NewBidMsg(_clientProtocol, _item);
            newBidMsg.send();

            while (true) {

                long msgID = Msg.receiveMsgId(_clientProtocol.getDIS());
                Utils.received(msgID);

                if (msgID == MagicNumbers.FINISH_MSG_NR) {
                    ServerIntf server;
                    server = _model.getServerByAddAndPort(
                            _clientProtocol.getIP(),
                            _clientProtocol.getPort());
                    if (server != null) {
                        ItemIntf item = null;
                        item = server.getItemByID(_item.getId());
                        if (item != null) {
                            item.finishAuction();
                        } else {
                            Utils.write("nie znalazłem Item, NewBIdOrder");
                        }
                    } else {
                        Utils.write("nie znalazłem serwera, NewBIdOrder");
                    }
                    break;

                } else if (msgID == MagicNumbers.BROADCAST_BID_MSG_NR) {
                    NewBidBroadcastMsg newBidBroadcastMsg =
                            new NewBidBroadcastMsg(_clientProtocol);
                    newBidBroadcastMsg.receive();
                    ItemIntf item = newBidBroadcastMsg.getItem();
                    _model.updateSerwerItemPrice(
                            _clientProtocol.getIP(),
                            _clientProtocol.getPort(),
                            item);
                    //nie przerywamy pętli!

                } else if (msgID == MagicNumbers.ACK_MSG_NR) {
                    _model.updateSerwerItemPrice(
                            _clientProtocol.getIP(),
                            _clientProtocol.getPort(),
                            _item);
                    break;

                } else if (msgID == MagicNumbers.NAK_MSG_NR) {
                    NakMsg nakMsg = new NakMsg(_clientProtocol);
                    nakMsg.receive();
                    Utils.write("Odrzucono propozycję podwyższenia ceny"
                            + nakMsg.getReason());
                    break;
                } else {
                    Utils.write("nieoczekiwany msg, NewBidOrder "+ msgID);
                    break;
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
